//
//  SettingViewController.swift
//  App_Profile
//
//  Created by 蒋晨成 on 2022/1/29.
//

import Adaptor
import Center_AppInfo
import Kingfisher
import Route
import RxCocoa
import RxSwift
import SnapKit
import Toast
import UICore
import UIKit

class SettingViewController: ViewController {
    lazy var listContentView: RYCollectionContentView = {
        let collectionViewLayout = UICollectionViewFlowLayout()
        collectionViewLayout.scrollDirection = .vertical
        collectionViewLayout.headerReferenceSize = CGSize(width: 300, height: 20)
        let listContentView = RYCollectionContentView(layout: collectionViewLayout)
        listContentView.backgroundColor = Adaptor.Profile.background
        listContentView.alwaysBounceVertical = true
        return listContentView
    }()
}

// MARK: - Override

extension SettingViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        setupNav()
        setup()
        bindData()
    }
}

private extension SettingViewController {
    func setupNav() {
        if (navigationController?.viewControllers.count ?? 0) > 1 {
            let backImage = ProfileModular.image(named: "Common.Nav.Action.Back")
            addDefaultLeftBarButtonItem(image: backImage)
        }
        setNavigationBarTintColor(tintColor: ProfileModular.color(name: "App.Dark1"))
        navigationItem.title = ProfileModular.localizedString(key: "Profile.Setting.Title")
    }

    func setup() {
        view.backgroundColor = Adaptor.Profile.background

        listContentView.x.add(to: view).snp.makeConstraints {
            $0.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            $0.leading.trailing.bottom.equalToSuperview()
        }
    }

    func bindData() {
        let notiCellModel = RYSwitchCellModel(title: ProfileModular.localizedString(key: "Profile.Setting.Notification"), isOn: false) { model, _ in
            guard let model = model as? RYSwitchCellModel else { return }
            logger.info("title:\(model.title) isOn:\(model.isOn)")
        }

        let clearCellModel = RYTextCellModel(title: ProfileModular.localizedString(key: "Profile.Setting.Cache")) { model in
            guard let model = model as? RYTextCellModel else { return }
            CacheManager.clear {
                model.text = "0B"
                self.listContentView.updateCellModel(cellModel: model)
            }
        }

        CacheManager.calculateDiskStorageSize { size in
            clearCellModel.text = size
            self.listContentView.updateCellModel(cellModel: clearCellModel)
        }

        let agreementCellModel = RYTitleCellModel(title: ProfileModular.localizedString(key: "Profile.Setting.Agreement")) { _ in
            Router.push(pattern: "WebBrowser/\(AppInfoCenter.agreementURL.urlEncode())")
        }

        let privacyCellModel = RYTitleCellModel(title: ProfileModular.localizedString(key: "Profile.Setting.Privacy")) { _ in
            Router.push(pattern: "WebBrowser/\(AppInfoCenter.privacyURL.urlEncode())")
        }

        let commitCellModel = RYTitleCellModel(title: ProfileModular.localizedString(key: "Profile.Setting.Commit")) { _ in
            Router.open(pattern: "appstore/2/\(AppInfoCenter.appID)")
        }

        let aboutCellModel = RYTextCellModel(
            title: "\(ProfileModular.localizedString(key: "Profile.Setting.About"))\(AppInfoCenter.appName)",
            text: "v\(AppInfoCenter.version).\(AppInfoCenter.build)"
        ) { _ in
            Router.push(pattern: "profile/setting/about")
        }

        let section0 = [notiCellModel, clearCellModel, agreementCellModel, privacyCellModel, commitCellModel, aboutCellModel]
        listContentView.ryDataList.append(section0)
        listContentView.reloadData()
    }
}

// MARK: - Routable

extension SettingViewController: Routable {
    static func initialize(values _: [String: Any]) -> ViewController? {
        SettingViewController(nibName: nil, bundle: nil)
    }

    static var routePattern = "profile/setting"

    static let description: String = "setting Router"
}
